package com.spm.librarianmanagersystem.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spm.librarianmanagersystem.mapper.BookMapper;
import com.spm.librarianmanagersystem.mapper.BorrowMapper;
import com.spm.librarianmanagersystem.pojo.Book;
import com.spm.librarianmanagersystem.pojo.Borrow;
import com.spm.librarianmanagersystem.service.BookService;
import com.spm.librarianmanagersystem.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: 苏成
 * @Date: 2022/04/07/12:42
 * @Description:
 */
@Service("BookService")
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bom;

    @Autowired
    private BorrowMapper borrowMapper;


    @Override
    public void updateBookLocation(String ISBN, Integer bookId) {
        bom.updateBookLocation(ISBN, bookId);
    }
    @Override
    public JSONArray getAllBook(int page_size, int current_page) {
        List<Book> bookList = bom.getAllBook();
        JSONArray array = new JSONArray();
        int all_size = bookList.size();

        // 根据是不是最后一页判断这一页有几本书
        JSONObject object1 = new JSONObject();
        int all = (int) Math.ceil( (double) all_size / page_size);
        int total = current_page < all ? page_size : ( all_size % page_size );
        object1.put("total", total);
        array.add(object1);

        int start = ( current_page - 1 )  * page_size;

        for( int i = 0 ; i < total && start + i < all_size ; i++ ){
            Book b = bookList.get(i+start);
            JSONObject object = new JSONObject();
            object.put("ISBN", b.getISBN());
            object.put("author", b.getAuthor());
            object.put("book_name", b.getBookName());
            object.put("status", b.getCategory());
            object.put("cover", b.getCover());
            object.put("published_time", b.getPublishedTime());
            object.put("publisher", b.getPublisher());
            object.put("brief_introduction",b.getBriefIntroduction());
            object.put("cover", b.getCover());

            object.put("class",b.getClass());
            array.add(object);
        }
        return array;
    }

    @Override
    public JSONArray getABook(String isbn) {
        Book book = bom.getABook(isbn);
        JSONArray array = new JSONArray();
        array.add( JSON.toJSON(book) );
        return array;
    }

    @Override
    public void updateBook(Book book) {
        bom.updateBook(book);
    }

    @Override
    public boolean addBook(Map map) {
        bom.insertBook(map);
        return true;
    }

    @Override
    public boolean delete(List ISBNS) {
        for(Object isbn : ISBNS){
            List<Integer> borrow_ids = bom.selectBorrowId(isbn.toString());
            for(Object id : borrow_ids )
                bom.deleteFine((Integer) id);
            bom.deleteBorrow(isbn.toString());
            bom.deleteBookId(isbn.toString());
            bom.deleteRes(isbn.toString());
            bom.deleteBook(isbn.toString());
        }
        return false;
    }

    @Override
    public boolean addBookBook(Map map) {
        int boodId = bom.countBook((String) map.get("ISBN"));
        int num = Integer.parseInt(String.valueOf(map.get("Num")));
        for(int i = 0 ; i < num ; i++){
            map.put("book_id", boodId+i+1);
            bom.addBookBook(map);
        }
        return true;
    }

    public boolean updateScore(String ISBN){
        Double bookScore = borrowMapper.getAverageScoreByBookId(ISBN);
        bom.updateScore(ISBN, bookScore);
        return true;
    }

    @Override
    public JSONArray getBorrowedBook() {
        JSONArray array = new JSONArray();
        List<Book> borrowedBook = bom.getBorrowedBook();
//        System.out.println(12123132);
//        System.out.println(borrowedBook);
        for(Book book : borrowedBook){
            JSONObject object = new JSONObject();
            object.put("ISBN", book.getISBN());
            object.put("author", book.getAuthor());
            object.put("book_name", book.getBookName());
            object.put("cover", book.getCover());
            object.put("published_time", book.getPublishedTime());
            object.put("publisher", book.getPublisher());
            object.put("brief_introduction",book.getBriefIntroduction());
            object.put("class",book.getCategory());
            array.add(object);
        }

        return array;
    }

    @Override
    public JSONArray getMostPopularBook() {
        JSONArray array = new JSONArray();
        List<Book> mostPopularBook = bom.getMostPopularBook();
//        System.out.println(12123132);
//        System.out.println(borrowedBook);
        for(Book book : mostPopularBook){
            JSONObject object = new JSONObject();
            object.put("ISBN", book.getISBN());
            object.put("author", book.getAuthor());
            object.put("book_name", book.getBookName());
            object.put("cover", book.getCover());
            object.put("published_time", book.getPublishedTime());
            object.put("publisher", book.getPublisher());
            object.put("brief_introduction",book.getBriefIntroduction());
            object.put("class",book.getCategory());
            array.add(object);
        }

        return array;
    }

}
